var fourSum = function(nums, target) {
    let n=nums.length,
        res=[];
    if(n<4) return res;
    nums.sort((a,b)=>a-b)

    for(let i=0;i<n-3;i++){
        //去重a
        if(i>0&&nums[i]==nums[i-1]) continue;
        for(let j=i+1;j<n-2;j++){
            //去重b
            if((j>i+1&&nums[j]==nums[j-1])) continue;
            let l =j+1,
                r=n-1;
            while(r>l){
                let sum = nums[i] +nums[j]+nums[l]+nums[r]
                if(sum>target) {r--;continue}
                if(sum<target){l++;continue}
                res.push([nums[i],nums[j],nums[l],nums[r]])
                //去重c
                while(r>l&&nums[l]===nums[++l]);
                while(r>l&&nums[r]===nums[--r]);
            }
        }
    }
    return res
};
console.log(fourSum([-2,-1,-1,1,1,2,2],0));